import EventEmitter from 'events'

export class MyEventEmitter {
    #eventName
    #listener
    #eventEmitter = new EventEmitter();

    constructor(eventName, listener) {
        this.#eventName = eventName
        this.#listener = listener

        this.#eventEmitter.setMaxListeners(0);
        this.#eventEmitter.on(this.#eventName, listener);
        setInterval(this.#emit, 1000);
    }

    closeListener = () => {
        // console.log(events.eventNames())
        console.log(`Listener count before off: ${this.#eventEmitter.listenerCount(this.#eventName)}`)
        this.#eventEmitter.off(this.#eventName, this.#listener)
        console.log(`Listener count after off: ${this.#eventEmitter.listenerCount(this.#eventName)}`)
    }

    #emit = () => {
        console.log('events.emit ...')
        this.#eventEmitter.emit(this.#eventName, new Date());
    }
}

// const events = new MyEventEmitter('Data', console.log);
// events.listener = console.log
